package com.wsoft.hr.pm.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.core.exception.Asserts;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.entity.HrPmActivityEntity;
import com.wsoft.hr.pm.entity.HrPmMemberTemplateEntity;
import com.wsoft.hr.pm.mapper.HrPmActivityMapper;
import com.wsoft.hr.pm.query.activity.member.MemberQuery;
import com.wsoft.hr.pm.query.activity.member.template.MemberTemplateInsertQuery;
import com.wsoft.hr.pm.query.activity.member.template.MemberTemplateQuery;
import com.wsoft.hr.pm.query.activity.member.template.PersonListQuery;
import com.wsoft.hr.pm.service.IHrPmMemberTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author z001
 * @Date 2025/3/3 08:25
 */
@Service
public class HrPmActivityManager extends ServiceImpl<HrPmActivityMapper, HrPmActivityEntity> {

    @Resource
    private IHrPmMemberTemplateService memberTemplateService;
    /**
     * 根据周期id  和 组织id查询
     * @param cycleId
     * @param orgId
     * @return
     */
    public List<HrPmActivityEntity> getByCycleAndOrg(Long cycleId,Long orgId,Integer status){
        QueryWrapper<HrPmActivityEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmActivityEntity::getCycleId,cycleId)
                .eq(HrPmActivityEntity::getHrOrgId,orgId)
                .eq(null != status,HrPmActivityEntity::getStatus,status);
        return list(query);
    }

    /**
     * 根据周期id集合查询
     * @param cycleIds
     * @param status
     * @return
     */
    public List<HrPmActivityEntity> listByCyclesNotStatus(List<Long> cycleIds,Integer status){
        if(CollUtil.isEmpty(cycleIds)){
            return new ArrayList<>();
        }
        QueryWrapper<HrPmActivityEntity> query = new QueryWrapper<>();
        query.lambda().in(HrPmActivityEntity::getCycleId,cycleIds)
                .ne(null != status,HrPmActivityEntity::getStatus,status);
        return list(query);
    }


    public List<HrPmActivityEntity> listActivity(PersonListQuery query) {
        QueryWrapper<HrPmActivityEntity> activityQuery = new QueryWrapper<>();
        activityQuery.lambda().like(StrUtil.isNotBlank(query.getName()), HrPmActivityEntity::getName, query.getName())
                .eq(HrPmActivityEntity::getStatus,1)
                .eq(null!=query.getCycleId(), HrPmActivityEntity::getCycleId, query.getCycleId());
        return list(activityQuery);
    }

    public void saveBatchMemberTemplate(Long activityId,List<MemberTemplateQuery> memberTemplateList){
        memberTemplateList.forEach(item->{
                    item.getMemberList().forEach(e->{
                        MemberTemplateInsertQuery insertQuery = new MemberTemplateInsertQuery();
                        insertQuery.setActivityId(activityId);
                        insertQuery.setMemberId(e.getMemberId());
                        insertQuery.setMemberName(e.getMemberName());
                        insertQuery.setMemberCode(e.getCode());
                        insertQuery.setHrOrgId(e.getHrOrgId());
                        insertQuery.setPostId(e.getPostId());
                        insertQuery.setPostName(e.getPostName());
                        insertQuery.setLevelId(e.getLevelId());
                        insertQuery.setLevelName(e.getLevelName());
                        insertQuery.setTemplateId(item.getTemplate().getTemplateId());
                        insertQuery.setTemplateName(item.getTemplate().getTemplateName());
                        memberTemplateService.insert(insertQuery);
                    });
        });
        }

    /**
     * 校验同一个人在同一个周期不能有两个活动
     * @param cycleId
     * @param memberTemplateList
     */
    public void checkCycle(Long cycleId, List<MemberTemplateQuery> memberTemplateList){
        //同一个人在同一个周期不能有两个活动
        QueryWrapper<HrPmActivityEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HrPmActivityEntity::getCycleId,cycleId);
        List<HrPmActivityEntity> list = list(queryWrapper);
        if (CollUtil.isNotEmpty(list)){
            List<Long> activityIds = list.stream().map(BaseEntity::getId).collect(Collectors.toList());
            QueryWrapper<HrPmMemberTemplateEntity> memberTemplateQueryWrapper = new QueryWrapper<>();
            memberTemplateQueryWrapper.lambda().in(HrPmMemberTemplateEntity::getActivityId,activityIds);
            List<HrPmMemberTemplateEntity> memberTemplateEntityList = memberTemplateService.list(memberTemplateQueryWrapper);
            if (CollUtil.isNotEmpty(memberTemplateEntityList)){
                List<Long> ids = memberTemplateEntityList.stream().map(HrPmMemberTemplateEntity::getMemberId).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(memberTemplateList)){
                    memberTemplateList.forEach(e->{
                        e.getMemberList().stream()
                                .map(MemberQuery::getMemberId)
                                .forEach(memberId -> {
                                    if (ids.contains(memberId)) {
                                        Asserts.fail("当前用户列表中已参加该考核周期的活动");
                                    }
                                });
                    });
                }
            }
        }
    }
}
